﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using Catering_DBOpertion;
using System.Data.SqlClient;

namespace Catering.DisplayBoards
{
    public partial class SatellitePlatformBoard : System.Web.UI.Page
    {
        Catering_Rules.Moon tMoon = new Catering_Rules.Moon();//微信平台字典
        Catering_Rules.ClassInfo tClassInfo = new Catering_Rules.ClassInfo();//班组管理
        Catering_Rules.DailyMoon tDailyMoon = new Catering_Rules.DailyMoon();//卫星平台动态
        Catering_Rules.Dynamics tDynamics = new Catering_Rules.Dynamics();//服务人员

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RepDataBind();
            }
        }

        /// <summary>
        /// 数据绑定
        /// </summary>
        public void RepDataBind()
        {
            DateTime SearchDateTime = DateTime.Now.AddDays(-1);
            if (Request.QueryString["Date"] != null)
            {
                SearchDateTime = Convert.ToDateTime(Request.QueryString["Date"]);
            }

            int AreaID = int.Parse(Request.QueryString["AreaID"].ToString()); //区域ID
            //获取对应班组集合
            List<Catering_Model.ClassInfo> ClassInfoList = tClassInfo.GetModelList("AreaID=" + AreaID);
            //获取所有班组对应的卫星平台动态
            List<Catering_Model.DailyMoon> DailyMoonList = tDailyMoon.GetModelList("AddDate=CAST('" + SearchDateTime.ToShortDateString() + "' as date)");
            //需绑定的数据源
            DataTable list = new DataTable();
            #region 添加表头
            list.Columns.Add("id");
            list.Columns.Add("DailyManagementID");
            list.Columns.Add("GroupID");
            list.Columns.Add("ClassID");
            list.Columns.Add("MoonID");
            list.Columns.Add("DailyID");
            list.Columns.Add("AddDate");
            list.Columns.Add("WoJu");
            list.Columns.Add("CanJu");
            list.Columns.Add("Rice");
            list.Columns.Add("Surfice");
            list.Columns.Add("Oil");
            list.Columns.Add("DishDate");
            list.Columns.Add("CheckDate");
            list.Columns.Add("IsCommunicate");
            list.Columns.Add("Area");
            list.Columns.Add("Oilstove");
            list.Columns.Add("SmokeMachineWash");
            list.Columns.Add("FireExtinguisher");
            list.Columns.Add("FireBlanket");
            list.Columns.Add("HighRiskEqui");
            list.Columns.Add("EquiOperation");
            list.Columns.Add("inWorkNum");
            list.Columns.Add("unWorkNum");
            list.Columns.Add("ClassName");
            list.Columns.Add("MoonName");
            list.Columns.Add("EatSum");
            list.Columns.Add("DaobanSum");
            list.Columns.Add("mimianyou");
            list.Columns.Add("goutong");
            list.Columns.Add("shangcaishijian");
            list.Columns.Add("jianchashijian");
            list.Columns.Add("bedsnum");
            #endregion
            //需绑定的数据源
            //List<Catering_Model.DailyMoon> list = new List<Catering_Model.DailyMoon>();
            foreach (Catering_Model.ClassInfo eClassInfo in ClassInfoList)
            {
                DataTable dt_moon = new Catering_Rules.Moon().GetList(" ClassId = " + eClassInfo.id.ToString()).Tables[0];
                if (dt_moon.Rows.Count == 0)
                    continue;
                DataRow dr = list.NewRow();               
                for (int moonCount = 0; moonCount < dt_moon.Rows.Count;moonCount++ )
                {                    
                    DataTable dt = new Catering_Rules.DailyMoon().GetList(1, "DailyID like '%," + eClassInfo.id.ToString() + ",%' and MoonID =" + dt_moon.Rows[moonCount]["ID"], " id desc").Tables[0];
                    if (dt.Rows.Count == 0)
                    {                       
                        continue;
                    }                        
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataColumn dc in dt.Columns)
                        {
                            if (dc.ColumnName == "Rice" || dc.ColumnName == "Surfice" || dc.ColumnName == "Oil" || dc.ColumnName == "Area")
                            {
                                if (dt.Rows[0][dc.ColumnName].ToString() != "")
                                    dr[dc.ColumnName] += Convert.ToDouble(dt.Rows[0][dc.ColumnName].ToString()).ToString("F1") + "☉";
                                else
                                    dr[dc.ColumnName] += "0.0☉";
                            }
                            else if (dc.ColumnName == "Oilstove")
                            {
                                if (dt.Rows[0][dc.ColumnName].ToString() != "")
                                    dr[dc.ColumnName] += Convert.ToDouble(dt.Rows[0][dc.ColumnName].ToString()).ToString("F0") + "☉";
                                else
                                    dr[dc.ColumnName] += "0☉";
                            }

                            dr[dc.ColumnName] += dt.Rows[0][dc.ColumnName].ToString() + "☉";
                        }
                        if (dt.Rows[0]["IsCommunicate"]!=null)
                        {
                            if (dt.Rows[0]["IsCommunicate"].ToString() == "True")
                                dr["goutong"] += "是☉";
                            else
                                dr["goutong"] += "否☉";
                        }
                        if (dt.Rows[0]["DishDate"] != null)
                        {
                            if (dt.Rows[0]["DishDate"].ToString() != "")
                                dr["shangcaishijian"] += Convert.ToDateTime(dt.Rows[0]["DishDate"].ToString()).ToString("yyyy/MM/dd") + "☉"; 
                        }
                        if (dt.Rows[0]["CheckDate"] != null)
                        {
                            if (dt.Rows[0]["CheckDate"].ToString() != "")
                                dr["jianchashijian"] += Convert.ToDateTime(dt.Rows[0]["CheckDate"].ToString()).ToString("yyyy/MM/dd") + "☉";
                        }
                        dr["ClassName"] = eClassInfo.CName;                        
                        dr["MoonName"] += dt_moon.Rows[moonCount]["MoonName"].ToString() + "☉";                        
                    }
                }
                if (dr["ClassName"] != null && dr["ClassName"].ToString()!="")
                {
                    list.Rows.Add(dr); 
                }                           
            }

            this.Rep_List.DataSource = list;
            this.Rep_List.DataBind();

            ScriptManager.RegisterClientScriptBlock(this.UpdatePanel1, Page.GetType(), "script", "<script>$(function () { $(\".Table_2>tbody tr:odd\").addClass(\"Table_Tr_odd\"); });</script>", false);
        }
        public int GetDaobanSum(int ClassID, DateTime Dates)
        {
            StringBuilder strSql = new StringBuilder(" SELECT State FROM AttendanceRecord WHERE ClassID=@ClassID and Date=@Dates");
            SqlParameter[] parameters = {
					new SqlParameter("@ClassID", SqlDbType.Int,ClassID),
                    new SqlParameter("@Dates", SqlDbType.DateTime)};
            parameters[0].Value = ClassID;
            parameters[1].Value = Dates;
            Object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj != null)
            {
                return Convert.ToInt32(obj);
            }
            return 0;
        }
        //定时刷新页面
        protected void Btn_Ref_Click(object sender, EventArgs e)
        {
            this.RepDataBind();
        }
    }
}